package com.gd.iot.energy.service.datamanage.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.datamanage.LogIotArchivesstatePOMapper;
import com.gd.iot.energy.po.datamanage.LogIotArchivesstatePO;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.datamanage.vo.MeterStatusPageVO;
import com.gd.iot.energy.service.datamanage.LogIotArchivesstateService;
import com.gd.iot.energy.service.systemadmin.AreaOrganizationService;
import com.gd.iot.energy.util.CustomColumnUtil;
import com.gd.iot.energy.util.CustomStrUtil;
import com.gd.iot.energy.util.EnergyTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class LogIotArchivesstateServiceImpl extends ServiceImpl<LogIotArchivesstatePOMapper, LogIotArchivesstatePO> implements LogIotArchivesstateService {


    @Autowired
    private AreaOrganizationService areaOrganizationService;

    @Resource
    private LogIotArchivesstatePOMapper logIotArchivesstatePOMapper;

    /**
     * 表状态表头配置文件
     */
    private static final String METER_STATUS_LIST_COLUMN_FILE_NAME = "datamanage/meter_status_list.column";

    @Override
    public PageVO<MeterStatusPageVO> getNBMeterStatusPageVO(String customerSerialnumber, String customerName,
                                                            String archivesAddress, String voltageStart,
                                                            String voltageEnd, String beginDate,
                                                            String endDate, Integer valve, Integer areaId,
                                                            Integer pageSize, Integer pageNum,
                                                            String orderColumn, String orderType,
                                                            SystemConsts.EquipType equipType) {
        IPage<LogIotArchivesstatePO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LogIotArchivesstatePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        queryWrapper.eq("type", equipType.value);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        if (StrUtil.isNotBlank(customerSerialnumber)) {
            queryWrapper.like("archives_number", customerSerialnumber);
        }
        if (StrUtil.isNotBlank(customerName)) {
            queryWrapper.like("archives_name", customerName);
        }
        if (StrUtil.isNotBlank(archivesAddress)) {
            queryWrapper.like("archives_address", archivesAddress);
        }
        if (StrUtil.isNotBlank(voltageStart)) {
            queryWrapper.ge("voltage", Double.parseDouble(voltageStart));
        }
        if (StrUtil.isNotBlank(voltageEnd)) {
            queryWrapper.le("voltage", Double.parseDouble(voltageEnd));
        }
        if (StrUtil.isNotBlank(beginDate)) {
            queryWrapper.ge(true, "meter_readingtime", DateUtil.beginOfDay(DateUtil.parseDate(beginDate)));
        }
        if (StrUtil.isNotBlank(endDate)) {
            queryWrapper.le(true, "meter_readingtime", DateUtil.endOfDay(DateUtil.parseDate(endDate)));
        }

        if (valve != null) {
            queryWrapper.eq("valve", valve);
        }

        if (areaId != null) {
            List<Long> allChildAreaIds = areaOrganizationService.getAllChildAreaIds(areaId);
            if (CollUtil.isNotEmpty(allChildAreaIds)) {
                queryWrapper.in("area_id", allChildAreaIds);
            }
        }

        if (StrUtil.isNotBlank(orderColumn) && StrUtil.isNotBlank(orderType)) {
            orderColumn = CustomStrUtil.humpToLine(orderColumn);
            queryWrapper.orderBy(true, orderType.equals(SystemConsts.OrderType.ASC.value), orderColumn);
        }

        IPage<LogIotArchivesstatePO> logIotArchivesstatePOPage = logIotArchivesstatePOMapper.selectPage(page, queryWrapper);
        PageVO<MeterStatusPageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<MeterStatusPageVO> meterStatusPageVOS = convertToMeterStatusPageVOList(logIotArchivesstatePOPage.getRecords());
        pageVO.setData(meterStatusPageVOS);
        pageVO.setTotal(logIotArchivesstatePOPage.getTotal());
        pageVO.setPages(logIotArchivesstatePOPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(METER_STATUS_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    /**
     * vo 转dto
     *
     * @param records
     * @return
     */
    private List<MeterStatusPageVO> convertToMeterStatusPageVOList(List<LogIotArchivesstatePO> records) {
        List<MeterStatusPageVO> res = new ArrayList<>(records.size());
        MeterStatusPageVO meterStatusPageVO;
        long i = 1;
        for (LogIotArchivesstatePO record : records) {
            meterStatusPageVO = new MeterStatusPageVO();
            meterStatusPageVO.setId(record.getId());
            meterStatusPageVO.setSerialNum(i);
            meterStatusPageVO.setArchivesName(record.getArchivesName());
            meterStatusPageVO.setArchivesAddress(record.getArchivesAddress());
            meterStatusPageVO.setArchivesNumber(record.getArchivesNumber());
            meterStatusPageVO.setAreaName(record.getAreaName());
            meterStatusPageVO.setFreezeReading(record.getFreezeReading());
            meterStatusPageVO.setLevel(record.getLevel());
            meterStatusPageVO.setValve(SystemConsts.ValveType.getValueNameByValue(record.getValve()));
            meterStatusPageVO.setMeterReadingtime(record.getMeterReadingtime());
            meterStatusPageVO.setVoltage(record.getVoltage() == null ? null : record.getVoltage().toString());
            meterStatusPageVO.setSensitivity(record.getSensitivity());
            meterStatusPageVO.setSignalNoise(record.getSignalNoise());
            meterStatusPageVO.setSignals(record.getSignals());
            meterStatusPageVO.setType(SystemConsts.EquipType.getValueNameByValue(record.getType()));
            res.add(meterStatusPageVO);
            i++;
        }
        return res;
    }
}
